package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import service.tAreaService;
import service.tDeviceService;
import service.tUserAndtDeviceService;
import service.tUserService;

import com.opensymphony.xwork2.ActionContext;

import entity.PageBean;
import entity.tArea;
import entity.tDevice;
import entity.tUser;
import entity.tUserAndtDevice;

public class GettUserDetail {
	
	private String userId;
	private String userName;
	private String trueName;
	private String email;
	private String company;
	private String mobilePhone;
	private String phone;
	private Integer grade;
	private String tAreaId; 
	
	private int pageSize;
	private int page;    //第几页
    
    private PageBean pageBean;
    private tAreaService tareaService;
	private tUserService tuserService;
	private tUserAndtDeviceService uadservice;
	private tDeviceService tdeviceService;
	@SuppressWarnings("unchecked")
	public String get() throws IOException, ParseException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		System.out.println("----------经过GettUserDetail----------");
		
		request.getAttribute("userId");
		System.out.println(userId);
		List<tUserAndtDevice> li = new ArrayList<tUserAndtDevice>();
		List<tUser> tuser = (List<tUser>) tuserService.searchById(userId);
		List<tArea> lis = new ArrayList<tArea>();
		userName = tuser.get(0).getUserName();
		trueName = tuser.get(0).getTrueName();
		email = tuser.get(0).getEmail();
	
		mobilePhone = tuser.get(0).getMobilePhone();
		phone = tuser.get(0).getPhone();
		grade = tuser.get(0).getGrade();
		tAreaId = tuser.get(0).gettAreaId();
		
		System.out.println(tuser.get(0).getUserName());
		request.setAttribute("tuser", tuser);
		System.out.println(grade);
		if (grade == 2){
			
			this.pageBean = tdeviceService.searchAlltDevice(15,page);
			List<tDevice> list = pageBean.getList();
			JSONArray jsonArray = new JSONArray();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("mac", tdevice.getMac());
				t.put("deviceId", tdevice.getDeviceId());
				
				jsonArray.add(t);
			}
			System.out.println(jsonArray.toString());
			
			request.setAttribute("lists",list);
		}
		if (grade == 1){
			System.out.println(tAreaId);
			lis = tareaService.searchById(tAreaId);
			String name = lis.get(0).getName();
			this.pageBean = tdeviceService.searchBytArea(name,15,page);
			List<tDevice> list = pageBean.getList();
			JSONArray jsonArray = new JSONArray();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("mac", tdevice.getMac());
				t.put("deviceId", tdevice.getDeviceId());
				
				jsonArray.add(t);
			}
			System.out.println(jsonArray.toString());
			
			request.setAttribute("lists",list);
		}
		if (grade == 0){
			li = uadservice.searchByuserId(userId);
			List<tDevice> lists1=new ArrayList();
			 for (tUserAndtDevice uad : li){
				 String deviceId = uad.getDeviceId();
				 System.out.println(deviceId);
				 //List<tDevice> list = new ArrayList<tDevice>();
				 this.pageBean = tdeviceService.searchById(deviceId,15,page);
				 
				 List<tDevice> list = pageBean.getList();
				
				 String mac = list.get(0).getMac();
				 String deviceid = list.get(0).getDeviceId();
				 String deviceNo = list.get(0).getDeviceNo();
				 String deviceName = list.get(0).getDeviceName();
				 Timestamp addTime = list.get(0).getAddTime();
				 String deviceArea = list.get(0).getDeviceArea();
				 String place = list.get(0).getPlace();
				 String deviceDescribe = list.get(0).getDeviceDescribe();
				 
				 JSONObject t = new JSONObject();
					
				 t.put("mac", mac);
				 t.put("deviceId", deviceid);
				 t.put("deviceNo", deviceNo);
				 t.put("deviceName", deviceName);
				 t.put("addTime", addTime);
				 t.put("deviceArea", deviceArea);
				 t.put("place", place);
				 t.put("deviceDescribe", deviceDescribe);
				 t.put("userId", userId);
				 
				 System.out.println(t);
				 lists1.addAll(list);
				 
			 }
			 request.setAttribute("lists",lists1);
		}
		
		return "success";
		
		
	}
	
	public String getuserId() throws IOException, ParseException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		System.out.println("----------经过GettUserDetail----------");
		
		request.getAttribute("userId");
		System.out.println(userId);
		
		return "success";
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public tUserAndtDeviceService getUadservice() {
		return uadservice;
	}

	public void setUadservice(tUserAndtDeviceService uadservice) {
		this.uadservice = uadservice;
	}

	public tDeviceService getTdeviceService() {
		return tdeviceService;
	}

	public void setTdeviceService(tDeviceService tdeviceService) {
		this.tdeviceService = tdeviceService;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Integer getGrade() {
		return grade;
	}

	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	public String gettAreaId() {
		return tAreaId;
	}

	public void settAreaId(String tAreaId) {
		this.tAreaId = tAreaId;
	}

	public tAreaService getTareaService() {
		return tareaService;
	}

	public void setTareaService(tAreaService tareaService) {
		this.tareaService = tareaService;
	}
	

}
